/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.icode.controllers;

import com.icode.entities.Modules;
import com.icode.rmi.IModules;
import com.icode.utils.MenuItem;
import com.icode.utils.RMIRequest;
import java.rmi.RemoteException;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JMenu;
import javax.swing.JMenuBar;

/**
 *
 * @author Hot_Girls
 */
public class ModulesControllers extends RMIRequest<IModules> {
    public ModulesControllers(){
        super("modules");
    }
    
    public JMenuBar buildMenu(){
        JMenuBar mBar = new JMenuBar();
        try {
            Modules root = this.gate.fetchById(1);
            Set<Modules> childs = root.getModuleses();
            for(Modules m : childs){
                JMenu mn = new JMenu(m.getName().trim());
                if(m.getModuleses().size() > 0){ // If It has child
                    buildChildrenMenu(mn, m.getModuleses());
                }
                
                mBar.add(mn);
            }
        } catch (RemoteException ex) {
            Logger.getLogger(ModulesControllers.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        return mBar;
    }
    
    public void buildChildrenMenu(JMenu mn, Set<Modules> st){
        for(Modules mi : st){
            if(mi.getModuleses().size() > 0){
                JMenu mi1 = new JMenu(mi.getName().trim());
                buildChildrenMenu(mn, mi.getModuleses());
                
                mn.add(mi1);
            }
            else{
                MenuItem mi1 = new MenuItem(mi.getName().trim());
                mi1.setModule(mi);
                mn.add(mi1);
            }
        }
    }
}
